package com.cloud.controller;

import com.cloud.domain.TPhone;
import com.cloud.service.ITPhoneService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器 GUN
 * </p>
 *
 * @author jobob
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/domain/t-phone")
public class TPhoneController {
    @Autowired
    private ITPhoneService iTPhoneService;

    @RequestMapping("/getById")
    public TPhone getById(@RequestBody Long id) {
        if (null == id) {
            return null;
        }
        return iTPhoneService.getById(id);
    }

    @RequestMapping("/save")
    public boolean save(@RequestBody TPhone tPhone) {
        if (null == tPhone) {
            throw new RuntimeException("数据为空，插入失败");
        }
        return iTPhoneService.save(tPhone);
    }

    @RequestMapping("/delete")
    public boolean delete(@RequestBody Long id) {
        if (null == id) {
          throw new RuntimeException("数据为空，删除失败");
        }
        return iTPhoneService.removeById(id);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody TPhone tPhone) {
        if (null == tPhone) {
           throw new RuntimeException("数据为空，修改失败");
        }
        return iTPhoneService.updateById(tPhone);
    }

    @RequestMapping("/listByPage")
    public PageInfo<TPhone> listByPage(@RequestBody TPhone tPhone) {
        int pageNum = 1;
        int pageSize = 10;
        if (null != tPhone && tPhone.getPageNum() != null) {
            pageNum = tPhone.getPageNum();
        }
        if (null != tPhone && tPhone.getPageSize() != null) {
           pageSize = tPhone.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<TPhone>(iTPhoneService.list());
    }

    @RequestMapping("/listAll")
    public List<TPhone> listAll() {
        return iTPhoneService.list();
    }
}
